package com.foucus.myfouces.MVP.Presenler.Presenler_class;

import android.content.Context;
import android.util.Log;

import com.foucus.myfouces.Api.GankService;
import com.foucus.myfouces.Base.BaseReq;
import com.foucus.myfouces.Base.Setting;
import com.foucus.myfouces.MVP.Model.SquareVideoResp;
import com.foucus.myfouces.MVP.Presenler.IPresenler.ISquareVideoP;
import com.foucus.myfouces.MVP.View.IView.IHomePageFG;
import com.foucus.myfouces.MVP.View.IView.ISelectRecommentFG;
import com.foucus.myfouces.Resource.HttpUtil.ServiceFactory;
import com.foucus.myfouces.Resource.Util.GsonImpl;
import com.foucus.myfouces.Resource.Util.JsonUtil;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yf-06 on 2017/7/12.
 */

public class SquareVideoP implements ISquareVideoP {
    IHomePageFG iHomePageFG;
    ISelectRecommentFG iSelectRecommentFG;
    Context context;
    private List<SquareVideoResp.ResultBean.DataBean> dataBeanList = new ArrayList<>();

    public SquareVideoP(Context context, ISelectRecommentFG iSelectRecommentFG) {
        this.iSelectRecommentFG = iSelectRecommentFG;
        this.context = context;
    }

    public SquareVideoP(Context context, IHomePageFG iHomePageFG) {
        this.iHomePageFG = iHomePageFG;
        this.context = context;
    }


    @Override
    public void getSquareVideo(String type) {
        BaseReq req = new BaseReq();
        req.setValue("type", type);
        req.setValue("lng", "100.564596");
        req.setValue("lat", "26.997575");
        req.setValue("page", "1");
        req.setValue("page_size", "10");
        req.setValue("order", "id");
        req.setValue("sort", "desc");
        ServiceFactory
                .getInstance()
                .createService(GankService.class)
                .getSquareVideo(req.getSecret("获取广场数据"))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResponseBody>() {
                    @Override
                    public void onCompleted() {
                        if (iHomePageFG != null) {
                            iHomePageFG.onSquareVideoCompleted(dataBeanList);
                        }

                        if (iSelectRecommentFG != null) {
                            iSelectRecommentFG.onSquareVideoCompleted(dataBeanList);
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (iHomePageFG != null) {
                            iHomePageFG.onSquareVideoError();
                        }
                        if (iSelectRecommentFG != null) {
                            iSelectRecommentFG.onSquareVideoError();
                        }

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        String response = JsonUtil.JsonToStr(responseBody);
                        boolean isflag = Setting.isRight(context, response);
                        if (isflag) {
                            SquareVideoResp resp = GsonImpl.get().toObject(response, SquareVideoResp.class);
                            dataBeanList.clear();
                            dataBeanList.addAll(resp.getResult().getData());

                        }
                    }
                });

    }
}
